<?php

use wechat\models\WeVar;
use yii\web\View;

/**
 * User: shutao
 * @var View $this
 * @var $model WeVar
 */
$this->title = '编辑变量';
?>

<div>
    <form class="layui-form">
        <!-- 文本输入框 -->
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red">*</span> <?php echo $model->getAttributeLabel('name') ?>
            </label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name" required="" lay-verify="required|name"
                       placeholder=""
                       value="<?php echo $model->name ?>"
                       maxlength="30"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">只能使用小写字母，只多为 30 个字母</div>
        </div>

        <div class="layui-form-item">
            <label for="value" class="layui-form-label">
                <span class="x-red">*</span> <?php echo $model->getAttributeLabel('value') ?>
            </label>
            <div class="layui-input-block">
                <input type="text" name="value" id="value" required="" lay-verify="required"
                       placeholder=""
                       value="<?php echo $model->value ?>"
                       autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label"></label>
            <input name="_csrf" type="hidden" id="_csrf" value="<?= \Yii::$app->request->csrfToken ?>">
            <button class="layui-btn" lay-filter='save' lay-submit="">保存</button>
        </div>
    </form>
</div>


<script>

    layui.use(['form', 'laydate', 'jquery'], function () {
        var form = layui.form, laydate = layui.laydate, $ = layui.$;

        form.verify({
            name: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!/^[a-z]+$/.test(value)) {
                    return '变量名只能使用小写字母';
                }
                if (value.length > 30) {
                    return '变量名最大长度为 30';
                }
            }
        });
        //监听提交
        form.on('submit(save)', function (data) {
            $.ajax({
                type: "POST",
                url: '#',
                data: data.field,
                success: function (response, textStatus, xhr) {
                    layer.msg('成功', {icon: 6, time: 1000}, function () {
                        xlayer.close(response);
                    });
                },
                error: function (xhr, textStatus, errorThrown) {
                    // 1 正确, 2 错误(X), 3 询问，4 锁定, 5 不开心, 5 开心(笑脸)
                    layer.alert(textStatus + ':' + xhr.responseText, {icon: 2}, function (index) {
                        layer.close(index);
                    });
                },
                dataType: 'json'
            });
            return false;
        });
    });
</script>